﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Abp.AspNetZeroCore.Net;
using Abp.Collections.Extensions;
using Abp.Dependency;
using Camc.Quality.Dto;
using Camc.Quality.Quality;
using Camc.Quality.Storage;
using OfficeOpenXml;
using OfficeOpenXml.Drawing;
using OfficeOpenXml.Style;

namespace Camc.Quality.DataExporting.Excel.EpPlus
{
    public abstract class EpPlusExcelExporterBase : QualityServiceBase, ITransientDependency
    {
        private readonly ITempFileCacheManager _tempFileCacheManager;
        protected EpPlusExcelExporterBase(ITempFileCacheManager tempFileCacheManager)
        {
            _tempFileCacheManager = tempFileCacheManager;
        }

        protected FileDto CreateExcelPackage(string fileName, Action<ExcelPackage> creator)
        {
            var file = new FileDto(fileName, MimeTypeNames.ApplicationVndOpenxmlformatsOfficedocumentSpreadsheetmlSheet);
            file.FileToken = Guid.NewGuid().ToString();
            using (var excelPackage = new ExcelPackage())
            {
                creator(excelPackage);
                Save(excelPackage, file);
            }

            return file;
        }

        protected string CreateExcelPackageLocal(string fileName, Action<ExcelPackage> creator)
        {
            var file = new FileDto(fileName, MimeTypeNames.ApplicationVndOpenxmlformatsOfficedocumentSpreadsheetmlSheet);
            file.FileToken = Guid.NewGuid().ToString();
            string fileSavePath = Environment.CurrentDirectory + "/wwwroot/TempExcel/";
            if (!Directory.Exists(fileSavePath))
            {
                Directory.CreateDirectory(fileSavePath);
            }
            string path = Path.Combine(fileSavePath, fileName);
            using (var excelPackage = new ExcelPackage())
            {
                creator(excelPackage);
                FileInfo info = new FileInfo(path);
                excelPackage.SaveAs(info);
            }
            return ConfigHelper.GetAppSetting("App", "GetAppUrl") + "/TempExcel/" + fileName;
        }






        protected void AddHeader(ExcelWorksheet sheet, params string[] headerTexts)
        {
            if (headerTexts.IsNullOrEmpty())
            {
                return;
            }

            for (var i = 0; i < headerTexts.Length; i++)
            {
                AddHeader(sheet, i + 1, headerTexts[i]);
            }
        }
        protected void AddHeaderDynamic(ExcelWorksheet sheet, List<string> headerTexts)
        {
            if (headerTexts.IsNullOrEmpty())
            {
                return;
            }

            for (var i = 0; i < headerTexts.Count; i++)
            {
                AddHeader(sheet, i + 1, headerTexts[i]);
            }
        }
        protected void AddHeader(ExcelWorksheet sheet, int columnIndex, string headerText)
        {
            sheet.Cells[1, columnIndex].Value = headerText;
            sheet.Cells[1, columnIndex].Style.Font.Bold = true;
            sheet.Cells[1, columnIndex].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            sheet.Cells[1, columnIndex].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
        }





        protected void AddObjects<T>(ExcelWorksheet sheet, int startRowIndex, IList<T> items, params Func<T, object>[] propertySelectors)
        {
            if (items.IsNullOrEmpty() || propertySelectors.IsNullOrEmpty())
            {
                return;
            }
            try
            {
                sheet.Cells.Style.ShrinkToFit = true;
                for (var i = 0; i < items.Count; i++)
                {
                    for (var j = 0; j < propertySelectors.Length; j++)
                    {
                        sheet.Cells[i + startRowIndex, j + 1].Value = propertySelectors[j](items[i]);
                    }
                }
            }
            catch (Exception ex)
            {

                throw;
            }
        }


        protected void AddQualityObjects<T>(ExcelWorksheet sheet, int startRowIndex, IList<T> items, params Func<T, object>[] propertySelectors)
        {
            if (items.IsNullOrEmpty() || propertySelectors.IsNullOrEmpty())
            {
                return;
            }
            try
            {
                List<int> widths = new List<int>();
                sheet.Cells.Style.ShrinkToFit = true;
                string url = ConfigHelper.GetAppSetting("App", "GetAppUrl");
                string fileSavePath = Environment.CurrentDirectory + "/wwwroot";

                for (var i = 0; i < items.Count; i++)
                {
                    for (var j = 0; j < propertySelectors.Length; j++)
                    {
                        if (propertySelectors[j](items[i]) != null)
                        {
                            sheet.Cells[i + startRowIndex, j + 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            sheet.Cells[i + startRowIndex, j + 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            //判断是否是图片 然后 计算位置开始插入
                            if (propertySelectors[j](items[i]).ToString().ToLower().Contains(".jpeg") || propertySelectors[j](items[i]).ToString().ToLower().Contains(".png") || propertySelectors[j](items[i]).ToString().ToLower().Contains(".mp4"))
                            {
                                var dealStr = propertySelectors[j](items[i]).ToString();
                                if (!string.IsNullOrEmpty(dealStr))
                                {
                                    dealStr = dealStr.Replace(",", "，");
                                }

                                var photoList = dealStr.Split('，');
                                int pos = 20;

                                for (int k = 0; k < photoList.Length; k++)
                                {


                                    if (photoList[k].Contains("*"))
                                    {
                                        var onePhoto = photoList[k].Split('*');
                                        if (onePhoto.Length == 3)
                                        {

                                            var tempPhoto = onePhoto[2];
                                            string tempPhotoPath = tempPhoto.Replace(url, fileSavePath);
                                            var processesExplain = Guid.NewGuid().ToString().Substring(0, 4);
                                            var fLast = tempPhotoPath.LastIndexOf("/");
                                            if (File.Exists(tempPhotoPath))
                                            {
                                                sheet.Row(i + startRowIndex).Height = 150;//设置行高
                                                sheet.Row(i + startRowIndex).CustomHeight = true;
                                                var f = sheet.Column(j + 1).Width;
                                                var ff = 20 * photoList.Length;
                                                if (ff > sheet.Column(j + 1).Width)
                                                {
                                                    sheet.Column(j + 1).Width = ff;
                                                }
                                                string hyperLinkPath = tempPhotoPath.Replace(fileSavePath + "/DownLoadFileRootPath/", "./").Replace(fileSavePath + "/UploadFile/", "./");
                                                if (tempPhotoPath.ToLower().Contains(".png") || tempPhotoPath.ToLower().Contains(".jpeg"))
                                                {
                                                    Image tempImage = Image.FromFile(@tempPhotoPath);
                                                    int widthImage = tempImage.Width;
                                                    int heightImage = tempImage.Height;

                                                    decimal re = (100 / Convert.ToDecimal(widthImage));
                                                    var he = heightImage * re;
                                                    var heInt = Convert.ToInt32(he);

                                                    ExcelPicture picture = sheet.Drawings.AddPicture(onePhoto[0] + " " + onePhoto[1] + " " + tempPhotoPath.Substring(fLast) + "_" + processesExplain, tempImage, new ExcelHyperLink(hyperLinkPath.Replace("_thumbnail", ""), UriKind.Relative));
                                                    picture.SetPosition(i + startRowIndex - 1, 20, j, pos);//设置图片的位置
                                                    pos = pos + 120;
                                                    picture.SetSize(100, heInt);
                                                }
                                                else
                                                {
                                                    ExcelPicture picture = sheet.Drawings.AddPicture(onePhoto[0] + " " + onePhoto[1] + " " + tempPhotoPath.Substring(fLast) + "_" + processesExplain, Image.FromFile(@fileSavePath + "/video.png"), new ExcelHyperLink(hyperLinkPath, UriKind.Relative));
                                                    picture.SetPosition(i + startRowIndex - 1, 20, j, pos);//设置图片的位置
                                                    pos = pos + 120;
                                                    picture.SetSize(100, 100);
                                                }

                                            }
                                            else
                                            {

                                                if (tempPhotoPath.ToLower().Contains(".png") || tempPhotoPath.ToLower().Contains(".jpeg"))
                                                {
                                                    ExcelPicture picture = sheet.Drawings.AddPicture("没有找到此图片:" + onePhoto[0] + " " + onePhoto[1] + " " + tempPhotoPath.Substring(fLast) + "_" + processesExplain, Image.FromFile(@fileSavePath + "/photo.png"));//插入图片
                                                    picture.SetPosition(i + startRowIndex - 1, 20, j, pos);//设置图片的位置
                                                    pos = pos + 120;
                                                    picture.SetSize(100, 100);
                                                }
                                                else
                                                {
                                                    ExcelPicture picture = sheet.Drawings.AddPicture("没有找到此视频:" + onePhoto[0] + " " + onePhoto[1] + " " + tempPhotoPath.Substring(fLast) + "_" + processesExplain, Image.FromFile(@fileSavePath + "/video.png"));//插入图片
                                                    picture.SetPosition(i + startRowIndex - 1, 20, j, pos);//设置图片的位置
                                                    pos = pos + 120;
                                                    picture.SetSize(100, 100);
                                                }
                                            }

                                        }
                                        else
                                        {
                                            if (widths.FindIndex(d => d == j + 1) == -1)
                                            {
                                                sheet.Column(j + 1).AutoFit();
                                            }
                                            sheet.Cells[i + startRowIndex, j + 1].Value = propertySelectors[j](items[i]);
                                        }
                                    }
                                    else
                                    {

                                        var tempPhoto = photoList[k];
                                        string tempPhotoPath = tempPhoto.Replace(url, fileSavePath);//.Replace("_thumbnail", "");
                                        var processesExplain = Guid.NewGuid().ToString().Substring(0, 4);
                                        if (File.Exists(tempPhotoPath))
                                        {
                                            var fLast = tempPhotoPath.LastIndexOf("/");
                                            sheet.Row(i + startRowIndex).Height = 150;//设置行高
                                            sheet.Row(i + startRowIndex).CustomHeight = true;
                                            var f = sheet.Column(j + 1).Width;
                                            var ff = 20 * photoList.Length;
                                            if (ff > sheet.Column(j + 1).Width)
                                            {
                                                sheet.Column(j + 1).Width = ff;
                                            }

                                            string hyperLinkPath = tempPhotoPath.Replace(fileSavePath + "/DownLoadFileRootPath/", "./").Replace(fileSavePath + "/UploadFile/", "./");
                                            if (tempPhotoPath.ToLower().Contains(".png") || tempPhotoPath.ToLower().Contains(".jpeg"))
                                            {

                                                Image tempImage = Image.FromFile(@tempPhotoPath);
                                                int widthImage = tempImage.Width;
                                                int heightImage = tempImage.Height;

                                                decimal re = (100 / Convert.ToDecimal(widthImage));
                                                var he = heightImage * re;
                                                var heInt = Convert.ToInt32(he);

                                                ExcelPicture picture = sheet.Drawings.AddPicture(tempPhoto + "_" + processesExplain, tempImage, new ExcelHyperLink(hyperLinkPath.Replace("_thumbnail", ""), UriKind.Relative));
                                                picture.SetPosition(i + startRowIndex - 1, 20, j, pos);//设置图片的位置
                                                pos = pos + 120;
                                                picture.SetSize(100, heInt);
                                            }
                                            else
                                            {
                                                ExcelPicture picture = sheet.Drawings.AddPicture(tempPhoto + "_" + processesExplain, Image.FromFile(@fileSavePath + "/video.png"), new ExcelHyperLink(hyperLinkPath, UriKind.Relative));//插入图片
                                                picture.SetPosition(i + startRowIndex - 1, 20, j, pos);//设置图片的位置
                                                pos = pos + 120;
                                                picture.SetSize(100, 100);
                                            }
                                            //自动调整行高
                                            // worksheet.Column(1).Width = 15;
                                        }
                                        else
                                        {
                                            if (tempPhotoPath.ToLower().Contains(".png") || tempPhotoPath.ToLower().Contains(".jpeg"))
                                            {
                                                ExcelPicture picture = sheet.Drawings.AddPicture("没有找到此图片:" + tempPhoto + "_" + processesExplain, Image.FromFile(@fileSavePath + "/photo.png"));//插入图片
                                                picture.SetPosition(i + startRowIndex - 1, 20, j, pos);//设置图片的位置
                                                pos = pos + 120;
                                                picture.SetSize(100, 100);
                                            }
                                            else
                                            {
                                                ExcelPicture picture = sheet.Drawings.AddPicture("没有找到此视频:" + tempPhoto + "_" + processesExplain, Image.FromFile(@fileSavePath + "/video.png"));//插入图片
                                                picture.SetPosition(i + startRowIndex - 1, 20, j, pos);//设置图片的位置
                                                pos = pos + 120;
                                                picture.SetSize(100, 100);
                                            }
                                        }
                                    }
                                    widths.Add(j + 1);
                                }
                            }
                            else
                            {
                                if (widths.FindIndex(d => d == j + 1) == -1)
                                {
                                    sheet.Column(j + 1).AutoFit();
                                }
                                sheet.Cells[i + startRowIndex, j + 1].Value = propertySelectors[j](items[i]);

                            }
                        }
                        else
                        {

                            sheet.Cells[i + startRowIndex, j + 1].Value = propertySelectors[j](items[i]);
                        }
                    }
                }
            }
            catch (Exception ex)
            {

                throw;
            }
        }


        List<TimeSpan> timeSpans = new List<TimeSpan>();
        protected void AddQualityObjectsNew<T>(bool isFt, ExcelWorksheet sheet, int startRowIndex, IList<T> items, params Func<T, object>[] propertySelectors)
        {
            if (items.IsNullOrEmpty() || propertySelectors.IsNullOrEmpty())
            {
                return;
            }
            try
            {
                Image ReducedImage;
                Image bitmap;
                ExcelPicture picture;
                Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(() => { return false; });
                List<int> widths = new List<int>();
                sheet.Cells.Style.ShrinkToFit = true;
                string url = ConfigHelper.GetAppSetting("App", "GetAppUrl");
                string fileSavePath = Environment.CurrentDirectory + "/wwwroot";
                Stopwatch stopwatch = new Stopwatch();
                Task.Run(() =>
                {
                    for (var i = 0; i < items.Count; i++)
                    {
                        //stopwatch.Start();

                        sheet.Row(i + startRowIndex).Height = 100;//设置行高
                        for (var j = 0; j < propertySelectors.Length; j++)
                        {
                            if (isFt)
                            {
                                if (propertySelectors[j](items[i]) != null)
                                {
                                    sheet.Cells[i + startRowIndex, j + 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                                    sheet.Cells[i + startRowIndex, j + 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                                    //判断是否是图片 然后 计算位置开始插入
                                    if (propertySelectors[j](items[i]).ToString().ToLower().Contains(".jpeg") || propertySelectors[j](items[i]).ToString().ToLower().Contains(".png") || propertySelectors[j](items[i]).ToString().ToLower().Contains(".mp4") || propertySelectors[j](items[i]).ToString().ToLower().Contains(".jpg"))
                                    {


                                        var dealStr = propertySelectors[j](items[i]).ToString();
                                        if (!string.IsNullOrEmpty(dealStr))
                                        {
                                            dealStr = dealStr.Replace(",", "，");
                                        }

                                        var photoList = dealStr.Split('，');
                                        int pos = 20;

                                        for (int k = 0; k < photoList.Length; k++)
                                        {


                                            if (photoList[k].Contains("*"))
                                            {
                                                var onePhoto = photoList[k].Split('*');
                                                if (onePhoto.Length == 3)
                                                {

                                                    var tempPhoto = onePhoto[2];
                                                    string tempPhotoPath = tempPhoto.Replace(url, fileSavePath);
                                                    // string tempPhotoPathLink = tempPhoto;
                                                    var processesExplain = Guid.NewGuid().ToString().Substring(0, 4);
                                                    var fLast = tempPhotoPath.LastIndexOf("/");
                                                    if (File.Exists(tempPhotoPath))
                                                    {
                                                        sheet.Row(i + startRowIndex).CustomHeight = true;
                                                        //var f = sheet.Column(j + 1).Width;
                                                        var ff = 20 * photoList.Length;
                                                        if (ff > sheet.Column(j + 1).Width)
                                                        {
                                                            sheet.Column(j + 1).Width = ff;
                                                        }
                                                        string hyperLinkPath = tempPhotoPath.Replace(fileSavePath + "/DownLoadFileRootPath/", "./").Replace(fileSavePath + "/UploadFile/", "./");
                                                        if (tempPhotoPath.ToLower().Contains(".png") || tempPhotoPath.ToLower().Contains(".jpeg") || tempPhotoPath.ToLower().Contains(".jpg"))
                                                        {
                                                            Image image;
                                                            bitmap = Image.FromFile(@tempPhotoPath);
                                                            image = bitmap.GetThumbnailImage(100, 100, callb, IntPtr.Zero);
                                                            string str = onePhoto[0] + " " + onePhoto[1] + " " + tempPhotoPath.Substring(fLast) + "_" + processesExplain;
                                                            using (picture = sheet.Drawings.AddPicture(str, image, new ExcelHyperLink(@hyperLinkPath, UriKind.RelativeOrAbsolute)))
                                                            {
                                                                picture.SetPosition(i + startRowIndex - 1, 20, j, pos);//设置图片的位置
                                                                pos = pos + 120;
                                                                image.Dispose();
                                                                GC.Collect();
                                                                //picture.SetSize(100, 100);
                                                            }
                                                        }
                                                        else
                                                        {
                                                            Image image;
                                                            bitmap = Image.FromFile(@fileSavePath + "/video.png");
                                                            image = bitmap.GetThumbnailImage(100, 100, callb, IntPtr.Zero);
                                                            string str = onePhoto[0] + " " + onePhoto[1] + " " + tempPhotoPath.Substring(fLast) + "_" + processesExplain;
                                                            using (picture = sheet.Drawings.AddPicture(str, image, new ExcelHyperLink(@hyperLinkPath, UriKind.RelativeOrAbsolute)))
                                                            {
                                                                picture.SetPosition(i + startRowIndex - 1, 20, j, pos);//设置图片的位置
                                                                pos = pos + 120;
                                                                image.Dispose();
                                                                GC.Collect();
                                                                //     picture.SetSize(100, 100);
                                                            }
                                                        }

                                                    }
                                                    else
                                                    {

                                                        if (tempPhotoPath.ToLower().Contains(".png") || tempPhotoPath.ToLower().Contains(".jpeg") || tempPhotoPath.ToLower().Contains(".jpg"))
                                                        {
                                                            //ExcelPicture picture = sheet.Drawings.AddPicture("没有找到此图片:" + onePhoto[0] + " " + onePhoto[1] + " " + tempPhotoPath.Substring(fLast) + "_" + processesExplain, Image.FromFile(@fileSavePath + "/photo.png"));//插入图片
                                                            //picture.SetPosition(i + startRowIndex - 1, 20, j, pos);//设置图片的位置
                                                            //pos = pos + 120;
                                                            //picture.SetSize(100, 100);
                                                            var ff = 20 * photoList.Length;
                                                            if (ff > sheet.Column(j + 1).Width)
                                                            {
                                                                sheet.Column(j + 1).Width = ff;
                                                            }
                                                            using (picture = sheet.Drawings.AddPicture("没有找到此图片:" + onePhoto[0] + " " + onePhoto[1] + " " + tempPhotoPath.Substring(fLast) + "_" + processesExplain, Image.FromFile(@fileSavePath + "/photo.png")))
                                                            {
                                                                picture.SetPosition(i + startRowIndex - 1, 20, j, pos);//设置图片的位置
                                                                pos = pos + 120;
                                                                picture.SetSize(100, 100);
                                                               
                                                                GC.Collect();
                                                                //     picture.SetSize(100, 100);
                                                            }

                                                        }
                                                        else
                                                        {
                                                            //ExcelPicture picture = sheet.Drawings.AddPicture("没有找到此视频:" + onePhoto[0] + " " + onePhoto[1] + " " + tempPhotoPath.Substring(fLast) + "_" + processesExplain, Image.FromFile(@fileSavePath + "/video.png"));//插入图片
                                                            //picture.SetPosition(i + startRowIndex - 1, 20, j, pos);//设置图片的位置
                                                            //pos = pos + 120;
                                                            //picture.SetSize(100, 100);
                                                            var ff = 20 * photoList.Length;
                                                            if (ff > sheet.Column(j + 1).Width)
                                                            {
                                                                sheet.Column(j + 1).Width = ff;
                                                            }
                                                            using (picture = sheet.Drawings.AddPicture("没有找到此视频:" + onePhoto[0] + " " + onePhoto[1] + " " + tempPhotoPath.Substring(fLast) + "_" + processesExplain, Image.FromFile(@fileSavePath + "/video.png")))
                                                            {
                                                                picture.SetPosition(i + startRowIndex - 1, 20, j, pos);//设置图片的位置
                                                                pos = pos + 120;
                                                                picture.SetSize(100, 100);
                                                              
                                                                GC.Collect();
                                                                //     picture.SetSize(100, 100);
                                                            }
                                                        }
                                                    }

                                                }
                                                else
                                                {
                                                    if (widths.FindIndex(d => d == j + 1) == -1)
                                                    {
                                                        sheet.Column(j + 1).AutoFit();
                                                    }
                                                    sheet.Cells[i + startRowIndex, j + 1].Value = propertySelectors[j](items[i]);
                                                }
                                            }
                                            else
                                            {

                                                var tempPhoto = photoList[k];
                                                string tempPhotoPath = tempPhoto.Replace(url, fileSavePath).Replace("_thumbnail", "");
                                                // string tempPhotoPathLink = tempPhoto.Replace("_thumbnail", "");
                                                //.Replace("picture", "CopyFile_TC");//.Replace("_thumbnail", "");
                                                var processesExplain = Guid.NewGuid().ToString().Substring(0, 4);
                                                if (File.Exists(tempPhotoPath))
                                                {
                                                    var fLast = tempPhotoPath.LastIndexOf("/");
                                                    //  sheet.Row(i + startRowIndex).Height = 22;//设置行高
                                                    sheet.Row(i + startRowIndex).CustomHeight = true;
                                                    // var f = sheet.Column(j + 1).Width;
                                                    var ff = 20 * photoList.Length;
                                                    if (ff > sheet.Column(j + 1).Width)
                                                    {
                                                        sheet.Column(j + 1).Width = ff;
                                                    }

                                                    string hyperLinkPath = tempPhotoPath.Replace(fileSavePath + "/DownLoadFileRootPath/", "./").Replace(fileSavePath + "/UploadFile/", "./");
                                                    if (tempPhotoPath.ToLower().Contains(".png") || tempPhotoPath.ToLower().Contains(".jpeg") || tempPhotoPath.ToLower().Contains(".jpg"))
                                                    {
                                                        //ReducedImage = Bitmap.FromFile(@tempPhotoPath);
                                                        //ReducedImage = ReducedImage.GetThumbnailImage(30, 30, callb, IntPtr.Zero);
                                                        Image image;
                                                        bitmap = Image.FromFile(@tempPhotoPath);
                                                        image = bitmap.GetThumbnailImage(100, 100, callb, IntPtr.Zero);
                                                        string str = tempPhoto + "_" + processesExplain;
                                                        using (picture = sheet.Drawings.AddPicture(str, image, new ExcelHyperLink(@hyperLinkPath, UriKind.RelativeOrAbsolute)))
                                                        {
                                                            picture.SetPosition(i + startRowIndex - 1, 20, j, pos);//设置图片的位置
                                                            pos = pos + 120;
                                                            image.Dispose();
                                                            GC.Collect();
                                                            //    picture.SetSize(100, 100);
                                                        };

                                                    }
                                                    else
                                                    {
                                                        Image image;
                                                        bitmap = Image.FromFile(@fileSavePath + "/video.png");
                                                        image = (Bitmap)bitmap.GetThumbnailImage(100, 100, callb, IntPtr.Zero);
                                                        string str = tempPhoto + "_" + processesExplain;
                                                        using (picture = sheet.Drawings.AddPicture(str, image, new ExcelHyperLink(@hyperLinkPath, UriKind.RelativeOrAbsolute)))//插入图片)
                                                        {
                                                            picture.SetPosition(i + startRowIndex - 1, 20, j, pos);//设置图片的位置
                                                            pos = pos + 120;
                                                            image.Dispose();
                                                            GC.Collect();
                                                            //   picture.SetSize(100, 100);
                                                        }

                                                    }
                                                    //自动调整行高
                                                    // worksheet.Column(1).Width = 15;
                                                }
                                                else
                                                {
                                                    if (tempPhotoPath.ToLower().Contains(".png") || tempPhotoPath.ToLower().Contains(".jpeg")||tempPhotoPath.ToLower().Contains(".jpg"))
                                                    {
                                                        //ExcelPicture picture = sheet.Drawings.AddPicture("没有找到此图片:" + tempPhoto + "_" + processesExplain, Image.FromFile(@fileSavePath + "/photo.png"));//插入图片
                                                        //picture.SetPosition(i + startRowIndex - 1, 20, j, pos);//设置图片的位置
                                                        //pos = pos + 120;
                                                        //picture.SetSize(100, 100);
                                                        var ff = 20 * photoList.Length;
                                                        if (ff > sheet.Column(j + 1).Width)
                                                        {
                                                            sheet.Column(j + 1).Width = ff;
                                                        }
                                                        using (picture = sheet.Drawings.AddPicture("没有找到此图片:" + tempPhoto + "_" + processesExplain, Image.FromFile(@fileSavePath + "/photo.png")))
                                                        {
                                                            picture.SetPosition(i + startRowIndex - 1, 20, j, pos);//设置图片的位置
                                                            pos = pos + 120;
                                                            picture.SetSize(100, 100);
                                                            
                                                            GC.Collect();
                                                           
                                                        }
                                                    }
                                                    else
                                                    {
                                                        // ExcelPicture picture = sheet.Drawings.AddPicture("没有找到此视频:" + tempPhoto + "_" + processesExplain, Image.FromFile(@fileSavePath + "/video.png"));//插入图片
                                                        //picture.SetPosition(i + startRowIndex - 1, 20, j, pos);//设置图片的位置
                                                        //pos = pos + 120;
                                                        //picture.SetSize(100, 100);
                                                        var ff = 20 * photoList.Length;
                                                        if (ff > sheet.Column(j + 1).Width)
                                                        {
                                                            sheet.Column(j + 1).Width = ff;
                                                        }
                                                        using (picture = sheet.Drawings.AddPicture("没有找到此视频:" + tempPhoto + "_" + processesExplain, Image.FromFile(@fileSavePath + "/video.png")))
                                                        {
                                                            picture.SetPosition(i + startRowIndex - 1, 20, j, pos);//设置图片的位置
                                                            pos = pos + 120;
                                                            picture.SetSize(100, 100);
                                                          
                                                            GC.Collect();
                                                            //     picture.SetSize(100, 100);
                                                        }
                                                    }
                                                }
                                            }
                                            widths.Add(j + 1);
                                        }
                                    }
                                    else
                                    {
                                        if (widths.FindIndex(d => d == j + 1) == -1)
                                        {
                                            sheet.Column(j + 1).AutoFit();
                                        }
                                        sheet.Cells[i + startRowIndex, j + 1].Value = propertySelectors[j](items[i]);
                                    }
                                }
                                else
                                {
                                    sheet.Cells[i + startRowIndex, j + 1].Value = propertySelectors[j](items[i]);
                                }
                            }
                            else
                            {
                                //20220808曲冰修改
                                //sheet.Cells[i + startRowIndex, j + 1].Value = propertySelectors[j](items[i]);
                                if (propertySelectors[j](items[i]) != null)
                                {
                                    if (propertySelectors[j](items[i]).ToString().ToLower().Contains(".jpeg") || propertySelectors[j](items[i]).ToString().ToLower().Contains(".png") || propertySelectors[j](items[i]).ToString().ToLower().Contains(".mp4") || propertySelectors[j](items[i]).ToString().ToLower().Contains(".jpg"))
                                    {
                                        var dealStr = propertySelectors[j](items[i]).ToString();
                                        if (!string.IsNullOrEmpty(dealStr))
                                        {
                                            var tempPhoto = dealStr.Split(',');
                                            List<string> str = tempPhoto.Select(t => t.Split('/').Last()).ToList();
                                            string strJoin = string.Join(",", str);
                                            sheet.Cells[i + startRowIndex, j + 1].Value = strJoin;
                                        }
                                    }
                                    else
                                    {
                                        if (widths.FindIndex(d => d == j + 1) == -1)
                                        {
                                            sheet.Column(j + 1).AutoFit();
                                        }
                                        sheet.Cells[i + startRowIndex, j + 1].Value = propertySelectors[j](items[i]);
                                    }
                                }
                                else
                                {
                                    sheet.Cells[i + startRowIndex, j + 1].Value = propertySelectors[j](items[i]);
                                }
                            }
                        }
                        //stopwatch.Stop();
                        //timeSpans.Add(stopwatch.Elapsed);
                    }
                    //var str11 = timeSpans.Max();
                    //ConfigHelper.CreateLogText("最大：" + str11.ToString());
                }).Wait();
                //sheet.DefaultRowHeight = 40;
                //sheet.CustomHeight = true;
            }
            catch (Exception ex)
            {
                ConfigHelper.CreateLogText(ex.ToString());
                throw;
            }
        }
        /// <summary>
        /// 添加公共导出树结构API 2021/07/12 添加 Author：曲冰
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sheet"></param>
        /// <param name="startRowIndex"></param>
        /// <param name="col"></param>
        /// <param name="items"></param>
        /// <param name="propertySelectors"></param>
        protected void AddDynamicObjects<T>(ExcelWorksheet sheet, int startRowIndex, int col, List<T> items, params Func<T, object>[] propertySelectors) where T : new()
        {
            T en = new T();
            int k = 0;
            if (items.IsNullOrEmpty() || propertySelectors.IsNullOrEmpty())
            {
                return;
            }
            try
            {
                for (var i = 0; i < items.Count; i++)
                {
                    k = col;
                    for (var j = 0; j < propertySelectors.Length; j++)
                    {
                        if (j == 0)
                        {
                            int a = 0;
                            a = (int)en.GetType().GetProperty("LevelNum").GetValue(items[i], null);
                            sheet.Cells[i + startRowIndex, j + a].Value = propertySelectors[j](items[i]);
                        }
                        else
                        {
                            sheet.Cells[i + startRowIndex, k + 1].Value = propertySelectors[j](items[i]);
                            k++;
                        }
                    }
                }
            }
            catch (Exception ex)
            {

                throw;
            }
        }
        protected void Save(ExcelPackage excelPackage, FileDto file)
        {
            _tempFileCacheManager.SetFile(file.FileToken, excelPackage.GetAsByteArray());
        }
        //[DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
        //public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);


    }
}
